﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;
using System.Web.Security;

namespace WebSite.admin.company
{
    public partial class addCompanyUser : BaseAdmin
    {




        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                if (base.CompanyId == null)
                {
                    base.ValidateLoginRule(Gift.Model.ENUM.ERule.AddCompany);
                }
            }
            if (!base.IsPostBack && (base.Request.QueryString["uid"] != null))
            {
                this.txtLoginPassword.Required = false;
                this.txtConfirmPassword.Required = false;
                this.BuildForm();
            }
        }

        private void BuildForm()
        {
            string sql = "select top 1 *, isnull(AdminTypeId,0) as AdminUserTypeId from tbAdminLogin where Adminid=" + base.Request.QueryString["uid"];
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            this.txtLoginName.Text = table.Rows[0]["AdminUser"].ToString();
            this.txtUserName.Text = table.Rows[0]["AdminName"].ToString();
          
            this.txtRemark.Text = table.Rows[0]["AdminDesc"].ToString();
            if (this.rblUserType.Items.FindByValue(table.Rows[0]["AdminUserTypeId"].ToString()) != null)
            {
                this.rblUserType.Items.FindByValue(table.Rows[0]["AdminUserTypeId"].ToString()).Selected = true;
                this.rblUserType.Enabled = false;
            }
        }

        protected void btnSubmitForm1_Click(object sender, EventArgs e)
        {

            if (base.Request.QueryString["uid"] != null)
            {
                string str2;
                DataTable table = SQLAccess.GreatDs("SELECT AdminPwd FROM tbAdminLogin where Adminid=" + base.Request.QueryString["uid"] + "").Tables[0];
                if (re.replace(this.txtLoginPassword.Text).ToString() == table.Rows[0]["AdminPwd"].ToString())
                {
                    str2 = re.replace(this.txtLoginPassword.Text);
                }
                else
                {
                    str2 = FormsAuthentication.HashPasswordForStoringInConfigFile(re.replace(this.txtLoginPassword.Text), "MD5");
                }
                SQLAccess.DoSql("update tbAdminLogin set AdminUser='" + re.replace(this.txtLoginName.Text) + "',AdminPwd='" + str2 + "',AdminName='" + re.replace(this.txtUserName.Text) + "',AdminFlag=1,AdminDesc='" + re.replace(this.txtRemark.Text) + "' where Adminid=" + base.Request.QueryString["uid"] + "");
                ExtAspNet.Alert.Show("修改成功！");
               
            }
            else
            {
                DataTable table2 = SQLAccess.GreatDs("SELECT AdminId FROM tbAdminLogin where AdminUser='" + re.replace(this.txtLoginName.Text) + "'").Tables[0];
                if (table2.Rows.Count > 0)
                {
                    ExtAspNet.Alert.Show("对不起，此用户已存在，请用别的用户名！");
                }
                else
                {
                    SQLAccess.DoSql("insert into tbAdminLogin(AdminUser,AdminPwd,AdminName,AdminFlag,AdminDesc,AdminTypeId) values ('" + re.replace(this.txtLoginName.Text) + "','" + FormsAuthentication.HashPasswordForStoringInConfigFile(re.replace(this.txtLoginName.Text), "MD5") + "','" + re.replace(this.txtLoginPassword.Text) + "',1,'" + re.replace(this.txtRemark.Text) + "'," + this.rblUserType.SelectedItem.Value + ");"
                                    + string.Format(" insert into tbCompanyAdmin(CompanyId,AdminId,AdminTypeId) values({0},@@IDENTITY,{1});", Request.QueryString["id"], this.rblUserType.SelectedItem.Value));
                    ExtAspNet.Alert.Show("添加成功！");
                 
                }
            }
            ExtAspNet.PageContext.RegisterStartupScript(ExtAspNet.ActiveWindow.GetHidePostBackReference());
        }

    }
}